package com.prestamomo.vil.ppa.pmvilmedium.pmviluse;

import android.content.Context;
import android.content.SharedPreferences;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class PmvilSpMd {

    public static final String PMVP_FILE_NAME = "pmvp_locat_data";


    public static void putPmvaParam(Context pmvaContext, String pmvaKey, Object pmvaObject) {

        SharedPreferences pmvaSp = pmvaContext.getSharedPreferences(PMVP_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor pmvaEditor = pmvaSp.edit();

        if (pmvaObject instanceof String) {
            pmvaEditor.putString(pmvaKey, (String) pmvaObject);
        } else if (pmvaObject instanceof Integer) {
            pmvaEditor.putInt(pmvaKey, (Integer) pmvaObject);
        } else if (pmvaObject instanceof Boolean) {
            pmvaEditor.putBoolean(pmvaKey, (Boolean) pmvaObject);
        } else if (pmvaObject instanceof Float) {
            pmvaEditor.putFloat(pmvaKey, (Float) pmvaObject);
        } else if (pmvaObject instanceof Long) {
            pmvaEditor.putLong(pmvaKey, (Long) pmvaObject);
        } else {
            pmvaEditor.putString(pmvaKey, pmvaObject.toString());
        }
        SharedPmvaPrePferencesCompat.pmvaApply(pmvaEditor);
    }


    public static Object getPmvaParam(Context pmvaContext, String pmvaKey, Object pmvaDefaultObject) {
        SharedPreferences pmvaSp = pmvaContext.getSharedPreferences(PMVP_FILE_NAME, Context.MODE_PRIVATE);
        if (pmvaDefaultObject instanceof String) {
            return pmvaSp.getString(pmvaKey, (String) pmvaDefaultObject);
        } else if (pmvaDefaultObject instanceof Integer) {
            return pmvaSp.getInt(pmvaKey, (Integer) pmvaDefaultObject);
        } else if (pmvaDefaultObject instanceof Boolean) {
            return pmvaSp.getBoolean(pmvaKey, (Boolean) pmvaDefaultObject);
        } else if (pmvaDefaultObject instanceof Float) {
            return pmvaSp.getFloat(pmvaKey, (Float) pmvaDefaultObject);
        } else if (pmvaDefaultObject instanceof Long) {
            return pmvaSp.getLong(pmvaKey, (Long) pmvaDefaultObject);
        }

        return null;
    }


    private static class SharedPmvaPrePferencesCompat {
        private static final Method pmvaSApplyMethod = findPmvaApplyMethod();

        @SuppressWarnings({"unchecked", "rawtypes"})
        private static Method findPmvaApplyMethod() {
            try {
                Class pmvaClz = SharedPreferences.Editor.class;
                return pmvaClz.getMethod("pmvaApply");
            } catch (NoSuchMethodException e) {
            }finally {
                return null;
            }
        }

        public static void pmvaApply(SharedPreferences.Editor pmvaEditor) {
            try {
                if (pmvaSApplyMethod != null) {
                    pmvaSApplyMethod.invoke(pmvaEditor);
                    return;
                }
            } catch (IllegalArgumentException e) {
            } catch (IllegalAccessException e) {
            } catch (InvocationTargetException e) {
            }
            pmvaEditor.commit();
        }
    }
}

